package xyz.wbsite.dbtool.javafx.tool;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EntityReader {
    private File javaClass;

    private String domainName;
    private String moduleName;
    private List<String> importList;
    private List<String> notesList;
    private Set<String> deptReqList;
    private Set<String> deptEntList;
    private String className;
    private String fatherName;
    private List<String> body;
    private boolean hasList = false;
    private String findOrSearchflag = "0";
    private String Tclass = null;

    public EntityReader(File javaClass) {
        this.javaClass = javaClass;
        importList = new ArrayList<>();
        notesList = new ArrayList<>();
        deptReqList = new HashSet<>();
        deptEntList = new HashSet<>();
        body = new ArrayList<>();
        domainName = "";
        read();
    }

    private void read() {
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(javaClass), "utf-8"));

            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.matches("\\s*package\\s(.*);")) {
                    Pattern compile = Pattern.compile("\\s*package\\s(.*)\\.module\\.(.*).ent;");
                    Matcher matcher = compile.matcher(line);
                    if (matcher.find()) {
                        domainName = matcher.group(1);
                        moduleName = matcher.group(2);
                    }
                    continue;
                }
                if (line.matches("import\\s.*")) {
                    if (line.contains(".ent.")) {
                        Pattern compile = Pattern.compile("import\\s+(.*\\.ent\\..*);");
                        Matcher matcher = compile.matcher(line);
                        if (matcher.find()) {
                            String group = matcher.group(1);
                            deptEntList.add(group);
                        }
                    } else if (line.contains("frame")) {

                    } else if (line.contains("javax.validation")) {

                    } else if (line.contains("org.hibernate.validator")) {

                    } else if (line.contains(".excel.")) {

                    } else {
                        importList.add(line);
                    }
                    continue;
                }
                if ((line.contains("/**") || line.contains("*") || line.contains("*/")) && body.size() == 0) {
                    notesList.add(line);
                }

                if (line.matches("public class\\s+(.*)\\s+extends\\s+(\\w*)\\s*\\{")) {
                    Pattern compile = Pattern.compile("public class\\s+(.*)\\s+extends\\s+(\\w*)\\s*\\{");
                    Matcher matcher = compile.matcher(line);

                    if (matcher.find()) {
                        className = matcher.group(1);
                        fatherName = matcher.group(2);
                    }
                    continue;
                }
                if (className != null) {
                    if (line.contains("@Excel")) {
                        continue;
                    }

                    body.add(line);
                }
            }

            for (int i = body.size() - 1; i >= 0; i--) {
                if (body.get(i).contains("}")) {
                    body.set(i, body.get(i).replaceAll("}", ""));
                    break;
                }
            }
            if (body.size() > 1 && "".equals(body.get(0))) {
                body.remove(0);
            }
            if (body.size() > 1 && "".equals(body.get(body.size() - 1))) {
                body.remove(body.size() - 1);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public String getDomainName() {
        return domainName;
    }

    public void setDomainName(String domainName) {
        this.domainName = domainName;
    }

    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public List<String> getNotesList() {
        return notesList;
    }

    public void setNotesList(List<String> notesList) {
        this.notesList = notesList;
    }

    public List<String> getImportList() {
        return importList;
    }

    public void setImportList(List<String> importList) {
        this.importList = importList;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getFatherName() {
        return fatherName;
    }

    public void setFatherName(String fatherName) {
        this.fatherName = fatherName;
    }

    public List<String> getBody() {
        return body;
    }

    public void setBody(List<String> body) {
        this.body = body;
    }

    public File getJavaClass() {
        return javaClass;
    }

    public void setJavaClass(File javaClass) {
        this.javaClass = javaClass;
    }

    public boolean isHasList() {
        return hasList;
    }

    public void setHasList(boolean hasList) {
        this.hasList = hasList;
    }

    public String getTclass() {
        return Tclass;
    }

    public String getFindOrSearchflag() {
        return findOrSearchflag;
    }

    public void setFindOrSearchflag(String findOrSearchflag) {
        this.findOrSearchflag = findOrSearchflag;
    }

    public void setTclass(String tclass) {
        Tclass = tclass;
    }

    public Set<String> getDeptReqList() {
        return deptReqList;
    }

    public void setDeptReqList(Set<String> deptReqList) {
        this.deptReqList = deptReqList;
    }

    public Set<String> getDeptEntList() {
        return deptEntList;
    }

    public void setDeptEntList(Set<String> deptEntList) {
        this.deptEntList = deptEntList;
    }

    public static void main(String[] args) throws IOException {
        EntityReader javaClassReader = new EntityReader(new File("D:\\wangbing\\Project\\dbtool\\target\\project\\example-web\\src\\main\\java\\com\\example\\module\\wsys\\rsp\\DictFindResponse.java"));

        System.out.println();
    }
}
